/**
 * Copyright 2012 Fonfon.Fenceer
 * All rights reserved.
 * 
 * @project 
 * @author Fonfon.Fenceer
 * @version 1.0
 * @data 2012-3-18
 */
package com.guidecaptain.java.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guidecaptain.java.mapper.CertificateMapper;
import com.guidecaptain.java.model.pojo.Certificate;
import com.guidecaptain.java.model.vo.CertificateVO;
import com.guidecaptain.java.service.CertificateService;

/**
 * 
 * @author fenceer
 */
@Service("certificateService")
public class CertificateServiceImpl implements CertificateService {
	
	private Logger LOG = Logger.getLogger(CertificateServiceImpl.class);
	
	@Autowired
	private CertificateMapper certificateMapper;

	/**
	 * 添加证件信息
	 * @param
	 * @return 返回ID
	 */
	@Override
	public int insertCertificate(Certificate certificate) {
		//如果签注次数为0，则不需要记录签注次数，设置为-1
		if(certificate.getEndorseNum()==0){
			certificate.setEndorseNum(-1);
		}
		//如果空白页数为0，则不需要记录空白页数，设置为-1
		if(certificate.getPage()==0){
			certificate.setPage(-1);
		}
		
		this.certificateMapper.insertCertificate(certificate);

		return certificate.getCtfId();
	}

	/**
	 * @param
	 * @return
	 */
	@Override
	public int deleteCertificate(int ctfId) {

		return this.certificateMapper.deleteCertificate(ctfId);
	}

	/**
	 * @param
	 * @return
	 */
	@Override
	public int updateCertificate(Certificate certificate) {

		return this.certificateMapper.updateCertificate(certificate);
	}

	/**
	 * 根据证件ID查询证件信息
	 */
	@Override
	public CertificateVO queryCertificate(int ctfId) {
		Certificate certificate = new Certificate();
		certificate.setCtfId(ctfId);
		List<CertificateVO> certificates = certificateMapper.queryCertificate(certificate);

		return certificates.get(0);
	}

	/**
	 * 根据导游ID查询导游证件
	 */
	@Override
	public List<CertificateVO> queryCertificateList(int guideId) {
		Certificate certificate = new Certificate();
		certificate.setGuideId(guideId);

		return this.certificateMapper.queryCertificate(certificate);
	}

	/**
	 * 检查证件提醒信息
	 * 
	 * @param
	 * @return
	 */
	@Override
	public boolean checkCtfRemind(int guideId) {
		Certificate certificate = new Certificate();
		certificate.setGuideId(guideId);
		List<CertificateVO> certificateVOs = this.certificateMapper.queryCertificate(certificate);

		Date today = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		for (CertificateVO ctf : certificateVOs) {
			//检查空白页
			if (ctf.getPage() >= 0 && ctf.getPage() < 3) return false;
			
			//检查签注次数
			if (ctf.getEndorseNum() >= 0 && ctf.getEndorseNum() < 3) return false;
			
			//先检查签注有效期，再检查证件有效期
			if (ctf.getEndorseDate() != null) {
				try {
					today = df.parse(df.format(today));
				} catch (ParseException e) {
					LOG.error(e);
				}

				int days = (int) ((ctf.getEndorseDate().getTime() - today.getTime()) / (24 * 60 * 60 * 1000));
				if (days < ctf.getWarnDays()) {
					return false;
				}
			} else if (ctf.getCtfDate() != null) {
				try {
					today = df.parse(df.format(today));
				} catch (ParseException e) {
					LOG.error(e);
				}

				int days = (int) ((ctf.getCtfDate().getTime() - today.getTime()) / (24 * 60 * 60 * 1000));
				if (days < ctf.getWarnDays()) {
					return false;
				}
			}
		}

		return true;
	}
	
}
